
from functools import wraps # 验证完不改变函数的名称
from django.http import HttpResponseBadRequest
from django.views.generic import View
from django.core.exceptions import PermissionDenied

def ajax_required(f):
    """验证请求是否为AJAX请求"""
    @wraps(f)
    def wrap(request,*args,**kwargs):
        if not request.is_ajax():
            return HttpResponseBadRequest('不是AJAX请求!')
        return f(request,*args,**kwargs)
    return wrap

class AuthorRequiredMixin(View):
    """
    验证登录用户是否为动态的发布者
    """
    def dispatch(self, request, *args, **kwargs):
        # 状态和文章的实例都有user属性
       if self.get_object().user.username != self.request.user.username:
           raise PermissionDenied
       return super().dispatch(request,*args,**kwargs)